import {
  getProcessList,
  getTechnologyList
} from '@/api/process/mesProcessMaterial';
import { getDict } from '@/api/common';

export const technologyDialog = {
  data() {
    return {
      technologyDialogVisible: false,  // 工艺查询弹框隐藏显示
      getTechnologyList,  // 获取弹框数据接口
      technologySearchConfig: {
        inline: true,
        formDesc: {
          technologyCode: {
            type: 'input',
            layout: 8,
            label: $i18n.t('mesProcessMaterial.technologyCode')
          },
          technologyName: {
            type: 'input',
            layout: 8,
            label: $i18n.t('mesProcessMaterial.technologyName')
          },
          productCode: {
            type: 'input',
            layout: 8,
            label: $i18n.t('mesProcessMaterial.productCode')
          },
          productName: {
            type: 'input',
            layout: 8,
            label: $i18n.t('mesProcessMaterial.productName')
          },
          seriesCode: {
            type: 'input',
            layout: 8,
            label: $i18n.t('mesProcessMaterial.seriesCoding')
          },
          seriesName: {
            type: 'input',
            layout: 8,
            label: $i18n.t('mesProcessMaterial.seriesName')
          },
        }
      },
      technologyColumn: [
        {
          type: 'selection'
        },
        {
          type: 'index',
          label: $i18n.t('common.order'),
          width: 60
        },
        {
          prop: 'technologyCode',
          label: $i18n.t('mesProcessMaterial.technologyCode'),
          width: 160,
          showOverflowTooltip: true
        },
        {
          prop: 'technologyName',
          label: $i18n.t('mesProcessMaterial.technologyName'),
          width: 180,
          showOverflowTooltip: true
        },
        {
          prop: 'isDefaultTechnology',
          label: $i18n.t('mesProcessMaterial.isDefaultTechnology'),
          width: 120,
          render: (h, { row }) => {
            return (
              <span>{ row.isDefaultTechnology === '1' ?  $i18n.t('common.yes')  :  $i18n.t('common.no') }</span>
            );
          },
        },
        {
          prop: 'technologyType',
          label: $i18n.t('mesProcessMaterial.technologyType2'),
          width: 120,
          optionProp: {
            value: 'value',
            label: 'description'
          },
          option: async (data) => {
            const list = await getDict('technology_type');
            return list.data.result;
          },
        },
        {
          prop: 'actualCode',
          label: $i18n.t('mesProcessMaterial.productSeriesCode'),
          width: 180,
          showOverflowTooltip: true,
        },
        {
          prop: 'actualName',
          label: $i18n.t('mesProcessMaterial.productSeriesName'),
          width: 180,
          showOverflowTooltip: true,
        },
        {
          prop: 'versionNumber',
          label: $i18n.t('mesProcessMaterial.version'),
          width: 120
        }
      ],
      technologyDialogFooter: [  // 工艺查询dialog底部按钮
        {
          name: $i18n.t("common.cancel"),
        },
        {
          name: $i18n.t("common.ok"),
          type: 'primary',
          disabled: (data) => {
            return data.length !== 1;
          }
        },
      ],
    }
  },
  methods: {
    async getSelectedTechnology(data) {
      const res = await getDict('technology_type');
      const technologyType = res.data.result.reduce((obj, item) => {
        obj[item.value] = item.description;
        return obj;
      }, {});
      this.$set(this.formDataDialog, 'technologyName', data[0]['technologyName']);
      this.$set(this.formDataDialog, 'technologyType', technologyType[data[0]['technologyType']]);
      const optionsRes = await getProcessList('0', data[0].id);
      this.processOptions = optionsRes.data.result;
      // 工序重新选择
      this.$set(this.formDataDialog, 'technologyProcess', '');
      this.$set(this.formDataDialog, 'actualCode', data[0]['actualCode']);
      this.$set(this.formDataDialog, 'actualName', data[0]['actualName']);
      this.$set(this.formDataDialog, 'versionNumber', data[0]['versionNumber']);
      this.$set(this.formDataDialog, 'technologyId', data[0]['id']);
    },
    // 工艺查询dialog底部取消和确定按钮事件
    changeTechnologyDialog(data) {
      data.name === $i18n.t('common.ok') && this.getSelectedTechnology(data.rows);
      this.technologyDialogVisible = false;
    },
  }
}
